<template>
  <el-dialog
    title="提示"
    :visible="dialogVisible"
    @close='onClose'
    @opened='onOpend'
    width="50%">
    <!-- 表单内容 -->
    <el-form ref="formList" :rules="rules"  :model="formList" label-width="100px" style="width:450px;margin:auto">
      <el-form-item label="用户名" prop="username">
        <el-input  v-model.trim="formList.username"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="formList.email"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model.trim="formList.password"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-input v-model="formList.role"></el-input>
      </el-form-item>
      <el-form-item label="权限组名称">
        <el-select v-model="formList.permission_group_id" placeholder="请选择活动区域">
          <el-option
          v-for="item in userInfo"
          :key="item.id"
          :label="item.permission_group_title"
          :value="item.permission_group_id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model="formList.phone"></el-input>
      </el-form-item>
      <el-form-item label="介绍">
        <el-input type="textarea" v-model="formList.introduction" ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onClose()">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { add, update } from '@/api/base/users.js'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    userInfo: {
      type: Array,
      required: true
    },
    DialogList: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      formList: {
        avatar: '',
        email: '',
        introduction: '',
        password: '',
        permission_group_id: null,
        phone: '',
        role: '',
        sex: '1',
        username: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
          { min: 6, max: 16, message: '密码格式需要在6-16位', trigger: 'blur' }
        ]
      }
    }
  },

  created () {

  },

  methods: {
    // 关闭按钮
    onClose () {
      this.$refs.formList.resetFields()
      this.$emit('update:dialogVisible', false)
    },
    // 点击确定  表单校验 发请求
    async onSave () {
      await this.$refs.formList.validate()
      try {
        if (this.formList.id) {
          // 编辑
          await update(this.formList)
          this.$message.success('编辑成功')
        } else {
          // 添加
          this.formList.id = Math.round(Math.random() * 500)
          // console.log(this.formList)
          await add(this.formList)
          this.$message.success('添加成功')
        }
        this.$emit('update:dialogVisible', false)
        this.formList = {
          avatar: '',
          email: '',
          introduction: '',
          password: '',
          permission_group_id: null,
          phone: '',
          role: '',
          sex: '1',
          username: ''
        }
        this.$emit('updateLoadList')
      } catch (err) {
        console.dir(err)
        this.$message.error('添加失败')
      }
    },
    onOpend () {
      for (const k in this.DialogList) {
        if (k === 'permission_group_title' || k === 'is_deleted' || k === 'create_time' || k === 'last_update_time') {
          delete this.DialogList[k]
        }
      }
      // console.log(this.DialogList)
      this.formList = this.DialogList
      // console.log(this.formList)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
